<?php
class System extends CI_Controller{
	private $per_page_banner_index = 1;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn() ) redirect("/");
		if($this->session->userdata("type_user") != "superadmin") redirect("/");
		$this->load->model("model_banner");
		$this->load->model("model_home");
	}
	function about(){
		$data['title'] = TITLE.' - About';
		$data['tinymce'] = 'y';
		if($this->input->post("btnSave") ){
			$name = $this->input->post("name",true);
			$name = str_replace('src="../../','src="'.base_url(),$name);
			$this->model_home->saveSystem("about_us",$name);
			$data['pesan'] = "Saved";
		}
		//waktu di save, replace src="../../ dengan base_url().image_tinymce
		$data['content'] = $this->model_home->getSystemVariable("about_us");
		$this->load->view('admin/about',$data);
	}
	function privacy(){
		$data['title'] = TITLE.' - Privacy';
		$data['tinymce'] = 'y';
		if($this->input->post("btnSave") ){
			$name = $this->input->post("name",true);
			$name = str_replace('src="../../','src="'.base_url(),$name);
			$this->model_home->saveSystem("privacy",$name);
			$data['pesan'] = "Saved";
		}
		$data['content'] = $this->model_home->getSystemVariable("privacy");
		//waktu di save, replace src="../../ dengan base_url().image_tinymce
		
		$this->load->view('admin/privacy',$data);
	}
	function terms(){
		$data['title'] = TITLE.' - Terms and Condition';
		$data['tinymce'] = 'y';
		if($this->input->post("btnSave") ){
			$name = $this->input->post("name",true);
			$name = str_replace('src="../../','src="'.base_url(),$name);
			$this->model_home->saveSystem("terms_and_condition",$name);
			$data['pesan'] = "Saved";
		}
		$data['content'] = $this->model_home->getSystemVariable("terms_and_condition");
		//waktu di save, replace src="../../ dengan base_url().image_tinymce
		
		$this->load->view('admin/terms',$data);
	}
	function contact(){
		$data['title'] = TITLE.' - Contact Us';
		$data['tinymce'] = 'y';
		if($this->input->post("btnSave") ){
			$name = $this->input->post("name",true);
			$name = str_replace('src="../../','src="'.base_url(),$name);
			$this->model_home->saveSystem("contact",$name);
			$data['pesan'] = "Saved";
		}
		$data['content'] = $this->model_home->getSystemVariable("contact");
		//waktu di save, replace src="../../ dengan base_url().image_tinymce
		
		$this->load->view('admin/contact',$data);
	}
	function faq(){
		$data['title'] = TITLE.' - FAQ';
		$data['tinymce'] = 'y';
		$data['javascript'][] = "faq.js";
		if($this->input->post("btnSave") ){
			$hasil = $this->_saveFAQ();
			if($hasil){
				$question = $this->input->post("question",true);
				$answer = $this->input->post("answer",true);
				$answer = str_replace('src="../../','src="'.base_url(),$answer);
				//save to database..
				$this->model_home->addFAQ($question,$answer);
				$data['success'] = "Added New FAQ";
			}
		}
		//waktu di save, replace src="../../ dengan base_url().image_tinymce
		
		$allFAQ = $this->model_home->getFaq();
		$data['allFAQ'] = $allFAQ;
		$this->load->view('admin/faq',$data);
	}
	function delete_faq($id){
		$this->model_home->deleteFAQ($id);
		redirect("admin/system/faq");
	}
	
	function banner(){
		$data['active_admin_banner'] = 'y';
		if($this->input->post("btnSave")){
			$hasil = $this->_saveBanner();
			if($hasil == "sukses"){
				$data['pesan_sukses'] = 'New Banner has been added.';
				$this->form_validation->_field_data = array();
			}else if($hasil == false){
			}else{
				$data['pesan_upload'] = $hasil;
			}
		}
		$data['title'] = TITLE.' - Banner';
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "tinybox_setup.js";
		
		$this->load->model("model_news");
		$data['link_to']['news'] = $this->model_news->getNews();
		$this->load->model("model_product");
		$data['link_to']['product'] = $this->model_product->getProduct();
		
		$params['order_by'] = "order_number desc, id asc";
		$count = count($this->model_banner->getBannerList($params));
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/system/banner_load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_banner_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['order_by'] = "order_number desc, id asc";
		$params['limit_1'] = $this->per_page_banner_index;
		$params['limit_2'] = "0";
		$banner_list = $this->model_banner->getBannerList($params);
		$i = 0;
		foreach($banner_list as $list){
			$allbanner[$i] = $list;
			$linkto = $list['link'];
			if($linkto != ""){
				$link_ex = explode("/",$linkto);
				if($link_ex[0] == "news"){
					$detail = $this->model_news->getNews(array("news_id"=>$link_ex[2]));
					if(count($detail) > 0){
						$allbanner[$i]['link_to'] = "<a href='".base_url()."admin/news/edit/".$link_ex[2]."'>".$newsDetail[0]['news_title'] . " (News)</a>";
					}
				}
				if($link_ex[0] == "product"){
					$detail = $this->model_product->getProduct(array("product_id"=>$link_ex[2]));
					if(count($detail) > 0){
						$allbanner[$i]['link_to'] = "<a href='".base_url()."admin/product/detail/".$link_ex[2]."'>".$detail[0]['name'] . " (Product)</a>";
					}
				}
			}
			$i++;
		}
		
		$data['banner_list'] = $allbanner;
		$this->load->view('admin/banner',$data);
	}
	function banner_load($offset=0){
		$this->load->model("model_news");
		$this->load->model("model_product");
		
		$params['order_by'] = "order_number desc, id asc";
		$count = count($this->model_banner->getBannerList($params));
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/system/banner_load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_banner_index;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['order_by'] = "order_number desc, id asc";
		$params['limit_1'] = $this->per_page_banner_index;
		$params['limit_2'] = $offset;
		$banner_list = $this->model_banner->getBannerList($params);
		$i = 0;
		foreach($banner_list as $list){
			$allbanner[$i] = $list;
			$linkto = $list['link'];
			if($linkto != ""){
				$link_ex = explode("/",$linkto);
				if($link_ex[0] == "news"){
					$detail = $this->model_news->getNews(array("news_id"=>$link_ex[2]));
					if(count($detail) > 0){
						$allbanner[$i]['link_to'] = "<a href='".base_url()."admin/news/edit/".$link_ex[2]."'>".$newsDetail[0]['news_title'] . " (News)</a>";
					}
				}
				if($link_ex[0] == "product"){
					$detail = $this->model_product->getProduct(array("product_id"=>$link_ex[2]));
					if(count($detail) > 0){
						$allbanner[$i]['link_to'] = "<a href='".base_url()."admin/product/detail/".$link_ex[2]."'>".$detail[0]['name'] . " (Product)</a>";
					}
				}
			}
			$i++;
		}
		
		$data['banner_list'] = $allbanner;
		$this->load->view('admin/content_load/banner_load',$data);
	}
	
	function change_banner_sequence(){
		$banner_id = $this->input->post('banner_id',true);
		$urutan = $this->input->post('urutan',true);
		$this->model_banner->change_banner_sequence($banner_id,$urutan);
		print "sukses";
	}
	function delete_banner($banner_id=""){
		if($banner_id == "") redirect("admin/system/banner");
		$this->model_banner->delete($banner_id);
		redirect("admin/system/banner");
	}
	
	function _saveBanner(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'title',
                     'label'   => 'Title',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'link_to',
                     'label'   => 'Link To',
                     'rules'   => ''
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$hasil = $this->model_banner->addBanner();
			return $hasil;
		}
	}
	function _saveFAQ(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'question',
                     'label'   => 'Question',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'answer',
                     'label'   => 'Answer',
                     'rules'   => 'trim|required|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			return true;
		}
	}
}